<!DOCTYPE html>
<html>

<head>
    <title>谷歌地图</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
        /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */

        #map {
            height: 100%;
        }

        /* Optional: Makes the sample page fill the window. */

        html,
        body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <script>
        function initMap() {
            // The location of Uluru
            var uluru = {
                lat: 39.883,
                lng: 116.423
            };
            // The map, centered at Uluru
            var map = new google.maps.Map(
                document.getElementById('map'), {
                    zoom: 4,
                    center: uluru
                });
            // The marker, positioned at Uluru
            var marker = new google.maps.Marker({
                position: uluru,
                map: map,
                draggable: true
            });



            marker.addListener('dragend', function () {

                //alert(marker.getPosition());

                regeocoderz(marker.getPosition());

            });



            function regeocoderz(lnglatXY) { //逆地理编码
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode({
                    'location': lnglatXY
                }, function (results, status) {
                    if (status === 'OK') {
                        if (results[0]) {
alert(results[0].formatted_address)
                            console.log(results[0].formatted_address);

                        } else {
                            window.alert('No results found');
                        }
                    } else {
                        window.alert('Geocoder failed due to: ' + status);
                    }

                })

            }

        }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWIxuw8LzVvbu01wIIY_jDZF2cUEOGHxI&callback=initMap" async
        defer></script>
    <!-- <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWIxuw8LzVvbu01wIIY_jDZF2cUEOGHxI&callback=initMap&language=en" async
        defer></script> -->
</body>

</html>